package com.moon.interview.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moon.interview.common.BaseResponse;
import com.moon.interview.common.ResultUtils;
import com.moon.interview.exception.SysErrorCode;
import com.moon.interview.exception.ApplicationException;
import com.moon.interview.model.dto.user.*;
import com.moon.interview.model.entity.User;
import com.moon.interview.model.vo.UserVo;
import com.moon.interview.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户管理")
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private IUserService userService;

    @Operation(summary = "注册")
    @PostMapping("/registry")
    public BaseResponse registry(@RequestBody UserRegistryRequest userRegistryRequest) {
        if (userRegistryRequest == null) {
            throw new ApplicationException(SysErrorCode.PARAMS_ERROR);
        }
        Long id = userService.register(userRegistryRequest);
        return ResultUtils.success(id);
    }

    @Operation(summary = "登录")
    @PostMapping("/login")
    public BaseResponse login(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        if (userLoginRequest == null) {
            throw new ApplicationException(SysErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(userService.login(userLoginRequest, request));
    }

    @Operation(summary = "退出登录")
    @PostMapping("/logout")
    public BaseResponse<Boolean> logout() {
        return ResultUtils.success(userService.logout());
    }

    @Operation(summary = "获取登录信息")
    @GetMapping("/getLoginInfo")
    public BaseResponse<UserVo> getUserInfo() {
        return ResultUtils.success(userService.getUserInfo());
    }

    @Operation(summary = "分页查询用户")
    @PostMapping("/page")
    public BaseResponse<Page<User>> page(@RequestBody UserQueryRequest request) {
        return ResultUtils.success(userService.page(request));
    }

    @Operation(summary = "新增用户")
    @PostMapping("/add")
    public BaseResponse<Long> add(@RequestBody UserAddRequest request) {
        return ResultUtils.success(userService.addUser(request));
    }

    @Operation(summary = "更新用户")
    @PostMapping("/update")
    public BaseResponse<Boolean> update(@RequestBody UserUpdateRequest request) {
        return ResultUtils.success(userService.updateUser(request));
    }

    @Operation(summary = "删除用户")
    @GetMapping("/remove")
    public BaseResponse<Boolean> remove(@RequestParam Long id) {
        if (id == null) {
            throw new ApplicationException(SysErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(userService.removeUser(id));
    }

    @Operation(summary = "更改密码")
    @GetMapping("/getLoginInfo")
    public BaseResponse<Boolean> updatePassword(@RequestBody UserPasswordUpdateRequest updateRequest) {
        return ResultUtils.success(userService.updatePassword(updateRequest));
    }

    @Operation(summary = "用户签到")
    @GetMapping("/addSign")
    public BaseResponse<Boolean> addSign() {
        return ResultUtils.success(userService.addSign());
    }

    @Operation(summary = "获取年签到信息")
    @GetMapping("/getSignInfo")
    public BaseResponse<List<Integer>> getSignInfo(int year) {
        return ResultUtils.success(userService.getSignInfo(year));
    }

}
